package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class EmpServicetest {

    @Resource
    private EmpMapper empMapper;

    @Test
    public void testWrapperSelect () {

        /**
         * select * from emp where name like '' and salary >= 1000
         */
        // 1-声明复杂查询的wrapper --> QueryWrapper
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();

        // 2-组装查询条件
        queryWrapper
                .like("name", "李")
                .ge("salary", 1000);

        List<Emp> emps = empMapper.selectList(queryWrapper);

        // 3-返回结果打印
        emps.forEach(System.out::println);

    }

    @Test
    public void testLamdaUpdate () {

        // 1-创建lamdaUpdate对象
        LambdaUpdateWrapper<Emp> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        // 2-组装查询条件: update emp set salary = salary + 2000 where id in(4,5,6)

        Emp updayeEmp = new Emp();
        lambdaUpdateWrapper
                .in(Emp::getId, 4,5,6)
                .setSql("salary = salary + 2000");

        empMapper.update(updayeEmp, lambdaUpdateWrapper);

        // 3-打印结果
    }

    @Test
    public void testBatchDelete () {
        empMapper.deleteBatchIds(List.of(4,5,6));
    }
}
